using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioMgr : MonoSingleton<AudioMgr>,IManager
{
    private Dictionary<string, GameObject> audioItems;

    public void Init()
    {
        audioItems = new Dictionary<string, GameObject>();
    }

    public void UnInit()
    {
        using (var e=audioItems.GetEnumerator())
        {
            while (e.MoveNext())
            {
                e.Current.Value.GetComponent<AudioSource>().Stop();
            }
        }
        audioItems.Clear();
    }

    public void PlayBGM()
    {
        if (DataMgr.Instance.MusicEnable)
        {
            AudioSource audio = null;
            if (audioItems.ContainsKey("startpanel"))
            {
                audioItems["startpanel"].SetActive(true);
                audio = audioItems["startpanel"].RequireComponent<AudioSource>();
            }
            else
            {
                audio = CreateAudioItem("startpanel");
            }
            Loader.Instance.LoadAsyn<AudioClip>("Audio/" + "startpanel", (clip) =>
            {
                audio.loop = true;
                audio.clip = clip;
                audio.Play();
            });
        }
    }

    public void StopBGM()
    {
        StopAudio("startpanel");
    }

    public bool IsBGMPlaying()
    {
        return IsPlaying("startpanel");
    }

    public void PlayAudio(string audioName,bool isLoop)
    {
        if (!DataMgr.Instance.SoundEnable)
        {
            return;
        }
        AudioSource audio = null;
        if (audioItems.ContainsKey(audioName))
        {
            audioItems[audioName].SetActive(true);
            audio = audioItems[audioName].RequireComponent<AudioSource>();
        }
        else
        {
            audio = CreateAudioItem(audioName);
        }
        Loader.Instance.LoadAsyn<AudioClip>("Audio/" + audioName, (clip) =>
        {
            audio.loop = isLoop;
            audio.clip = clip;
            audio.Play();
        });
    }

    public void StopAudio(string audioName)
    {
        if (audioItems.ContainsKey(audioName))
        {
            audioItems[audioName].SetActive(false);
        }
    }

    public void StopAllAudioExceptBGM()
    {
        using (var e=audioItems.GetEnumerator())
        {
            while (e.MoveNext())
            {
                if (e.Current.Key!="startpanel")
                {
                    e.Current.Value.SetActive(false);
                }
            }
        }
    }

    public bool IsPlaying(string audioName)
    {
        if (!audioItems.ContainsKey(audioName))
        {
            return false;
        }
        return audioItems[audioName].GetComponent<AudioSource>().isPlaying;
    }

    private AudioSource CreateAudioItem(string itemName)
    {
        GameObject obj = new GameObject(itemName);
        obj.transform.SetParent(transform);
        audioItems.Add(itemName, obj);
        return obj.RequireComponent<AudioSource>();
    }
}
